$(function () {

    let currentPage = 1
    let perpage = 10
    let totalCount

    $('.showNum').on('change', function () {
        perpage = $(this).val()
        currentPage = 1
        init()
    })

    function init() {
        $.ajax({
            type: "get",
            url: BigNew.comment_list,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: { page: currentPage, perpage },
            dataType: "json",
            success: function (res) {
                // console.log(res)
                $("tbody").html(template('commentTemp', res))
                //总页数
                setPage(res.data.totalPage)
                //总评论数
                totalCount = res.data.totalCount
            }
        });
    }
    init()



    //设置分页
    function setPage(pageSum) {
        $(".pagination").bootstrapPaginator({
            //设置版本号
            bootstrapMajorVersion: 3,
            // 显示第几页
            currentPage: currentPage,
            // 总页数
            totalPages: pageSum,
            //当单击操作按钮的时候, 执行该函数, 调用ajax渲染页面
            onPageClicked: function (event, originalEvent, type, page) {
                // 把当前点击的页码赋值给currentPage, 调用ajax,渲染页面
                currentPage = page
                init()
            }
        })
    }

    let id
    function port(url) {
        $.ajax({
            type: "post",
            url,
            headers: { Authorization: localStorage.getItem('bignews_token') },
            data: { id },
            dataType: "json",
            success: function (res) {
                // console.log(res)
                alert(res.msg)
                init()
            }
        });
    }
    //删除
    $('tbody').on('click', '.btnDel', function () {

        if (totalCount % perpage == 1) {
            if (currentPage > 1) {
                currentPage--
            }
        }
        id = $(this).data().id
        if (confirm('请问是否删除')) {
            port(BigNew.comment_delete)
        }
    })


    //通过
    $('tbody').on('click', '.btnPass', function () {
        // console.log($(this).data().id)
        id = $(this).data().id
        port(BigNew.comment_pass)

    })

    //拒绝reject
    $('tbody').on('click', '.btnReject', function () {
        // console.log($(this).data().id)
        id = $(this).data().id
        port(BigNew.comment_reject)

    })


})